home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  1.2 KB  |  69 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4.  /* timings: */
  5.  /* -i: 18.5u */
  6.  /* -s: 14.2u   */
  7.  /* -n: 15.3u */
  8.  /* -f: 14.4u */
  9.  
  10. #include <stream.h>
  11.  
  12. typedef int Cell;
  13. const int SIZE = 1000000;
  14. Cell array[SIZE];
  15.  
  16. struct upstack {
  17.   Cell* sp;
  18.   Cell* sp0;
  19.   void init(Cell* p) {sp0 = sp = p;}
  20.   void init(Cell* bottom, Cell* top) {sp0 = bottom; sp = top;}
  21.   Cell* bottom() {return sp0;}
  22.   void push(Cell val) {*sp++ = val;}
  23.   Cell pop() {return *--sp;}
  24.   int nonempty() {return (sp > sp0);}
  25. };
  26.  
  27. upstack S;
  28.  
  29. main(int, char** argv)
  30. {
  31.   S.init(array);
  32.   upstack Local;
  33.   Local.init(array);
  34.   upstack* SP = &S;
  35.   Cell* p = array;
  36.   Cell var;
  37.   for (int j = 0; j < 5; j++) {
  38.     switch (argv[1][1]) {
  39.     case 'n':
  40.       for (int i = 0; i < SIZE; i++) {
  41.     S.push(i);
  42.     var = S.pop();
  43.       }
  44.       break;
  45.     case 'f':
  46.       for (i = 0; i < SIZE; i++) {
  47.     *p++ = i;
  48.     var = *--p;
  49.       }
  50.       break;
  51.     case 's':
  52.       for (i = 0; i < SIZE; i++) {
  53.     Local.push(i);
  54.     var = Local.pop();
  55.       }
  56.       break;
  57.     case 'i':
  58.       for (i = 0; i < SIZE; i++) {
  59.     SP->push(i);
  60.     var = SP->pop();
  61.       }
  62.       break;
  63.     case 'h':
  64.     default:
  65.       cout << "-[isnf] indirect, slow, normal, fast\n";
  66.     }
  67.   }
  68. }
  69.